﻿using System.Collections.Generic;
using System.Linq;
using Myotragus.Data.Containers;
using Myotragus.Data.Criteria;
using Myotragus.Data.Domain;

namespace Myotragus.Data.Repositories
{
	public class WriteRepository<TEntity, TId> : ReadRepository<TEntity, TId>,
		IWriteRepository<TEntity, TId>
		where TEntity : class, IEntity<TId>
	{
		public WriteRepository(IEntitiesContainer entities, IWriteQueryBuilder queryBuilder)
			: base(entities, queryBuilder)
		{
			WriteQueryBuilder = queryBuilder;
		}

		protected IWriteQueryBuilder WriteQueryBuilder { get; private set; }

		public void Save(IEnumerable<TEntity> entities)
		{
			WriteQueryBuilder.Save(entities);
		}

		public void Save(params TEntity[] entities)
		{
			Save((IEnumerable<TEntity>)entities);
		}

		public void Delete(IEnumerable<TId> ids)
		{
			Delete(ids.Select(id => FindById(id)));
		}

		public void Delete(params TId[] ids)
		{
			Delete((IEnumerable<TId>)ids);
		}

		public void Delete(IEnumerable<TEntity> entities)
		{
			WriteQueryBuilder.Delete(entities);
		}

		public void Delete(params TEntity[] entities)
		{
			Delete((IEnumerable<TEntity>)entities);
		}
	}
}